/*
 * Amkai Enterprise (AE) 
 * Copyright 2004-2006 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler.dictionary
 * File: DictionaryActivationHandler.java
 * Created: Feb 2, 2007 2:45:17 PM
 * Author: Miskolczi P�ter 
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler.dictionary;

import java.io.Serializable;

import com.amkai.aes.aescommon.dictionary.SharedDictionaryManagerCommon;
import com.amkai.aes.aescommon.service.binary.job.dictionary.DictionaryActivationJob;
import com.amkai.aes.generatedbeans.dictionary.Item;
import com.amkai.aes.generatedbeans.inventory.items.SupplyResourceItem;
import com.amkai.aes.generatedbeans.physstaff.Physician;
import com.amkai.aes.generatedbeans.procedure.Procedure;
import com.amkai.aes.server.dictionary.ServerContractManager;
import com.amkai.aes.server.dictionary.SharedDictionaryManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.rad.designerbase.beans.ChangedKind;

/**
 * DictionaryActivationHandler
 * @author Miskolczi P�ter
 * Description:
 *
 */
public class DictionaryActivationHandler implements JobObjectHandler<DictionaryActivationJob, Serializable, Serializable> {

	public Serializable process(DictionaryActivationJob job, Serializable object) throws Exception {
		SessionManager sm = SiteManager.Start();
		try {
			// KKORNEL: AES-4465
			/*if (!CommonSettings.USE_DATA_SHARING) {
				DictionaryManager dm = new DictionaryManager(sm);
				if (job.activation) {
					for(Long itemId : job.itemIds)
						dm.activateIfNeeded(itemId, job.baseOrganizationId);
				}
				else {
					for(Long itemId : job.itemIds) {
						dm.deactivate(itemId, job.baseOrganizationId);
					}
				}
			} else {*/
				SharedDictionaryManager sdm = new SharedDictionaryManager(sm);
				
				for(Long itemId : job.getItemIds()) {
					//Item item = sm.get(Item.class, itemId);
					// KKORNEL: AES-11533
					Item item = SharedDictionaryManager.getItemById(itemId);
					item.setActive(job.isActivation());
					// KKORNEL: AES-17796
					if (item instanceof Physician) {
						if (job.isSetProviderToPhysStaff()) {
							((Physician)item).setIsProvider(Boolean.TRUE);
						}
					}
					sm.openTransaction();
					if (SharedDictionaryManagerCommon.getInstance().isPropertyShared(item.getKind(), Item.PROPERTYNAME_ACTIVE)) {
						item.setChangedKind(ChangedKind.CHANGED);
						sdm.saveItem(item);
					}
					else sm.saveRAD(item);
					
					sm.commitTransaction();
					sm.clear();
					
					if (job.isActivation()) {
						// KKORNEL: AES-11414
						if (item instanceof Procedure) {
							// KKORNEL: AES-12394
							ServerContractManager.insertProcedure(sm, (Procedure)item, false);
						}
						else if (item instanceof SupplyResourceItem) {
							SupplyResourceItem sri = (SupplyResourceItem)item;
							if (sri.isBillableItem()) {
								sm.openTransaction();
								// KKORNEL: AES-12394
								ServerContractManager.insertSupplyResource(sm, sri, false);
								sm.commitTransaction();
							}
						}
					}
				}
			//}
		} finally {
			sm.closeSession();
		}
		return null;
	}

}
